<!-- Copyright (c) 2022 渝州大数据实验室
 *
 * Lanius is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 -->
<!--
* @description: 加密的密码输入框，基于el-input实现
* @fileName: lanius-password-input.vue
* @author: zoujunjie@yzbdl.ac.cn
* @date: 2022-09-21
-->
<template>
  <el-input
    type="password"
    :placeholder="props.placeholder"
    v-model="userInputedPassword"
  ></el-input>
</template>

<script lang="ts" setup>
import { decryptAes } from "@/services/common/tool";
import { watch, ref } from "vue";

interface IProps {
  placeholder?: string;
  size?: string;
  modelValue?: string;
  defaultValue?: string;
}

interface IEmits {
  (e: "update:modelValue", value: string): void;
}

const props = withDefaults(defineProps<IProps>(), {
  placeholder: "",
  modelValue: "",
  defaultValue: "",
});
const emits = defineEmits<IEmits>();

const userInputedPassword = ref("");

watch(
  [() => userInputedPassword.value, () => props.defaultValue],
  () => {
    if (props.defaultValue) {
      if (userInputedPassword.value) {
        emits("update:modelValue", userInputedPassword.value);
      } else {
        emits("update:modelValue", decryptAes(props.defaultValue));
      }
    } else {
      emits("update:modelValue", userInputedPassword.value);
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="scss" scoped></style>
